
var GameObject = Class.extend({
	x: 0,
	y: 0,
	id: 0,
    getID: function(){
        return this.id;
    },
	setPos: function(x_abs, y_abs) {
		this.x = x_abs * grid;
		this.y = y_abs * grid;
	},
	getPosX: function() {
		return Math.ceil(this.x/grid);
	},
	getPosY: function() {
		return Math.ceil(this.y/grid);
	}
});

var Player = GameObject.extend({
	x_move: 0,
	y_move: 0,
	type: PlayerType.HUMAN,
	speed: 10,
	lives: 3,
	kills: 0,
	maxBombsNumber: 3,
	currentBombsNumber: 0,
	assingedKeys: null,

	init: function(id) {
		this.id = id;
		console.log("SET ID: " + this.id);
	},
	moveX: function(n) {
		this.x_move += n * grid;
	},
	moveY: function(n) {
		this.y_move += n * grid;
	},
	isMoving: function() {
		return (this.x_move != 0 || this.y_move != 0);
	},
	isAlive: function() {
		if (this.lives > 0) {
			return true;
		}
		return false;
	},
	bombExploded: function() {
		this.currentBombsNumber --;
	},
	getActiveBombsCount: function() {
		return this.currentBombsNumber;
	},
	removeLife: function() {
		if (!this.lives == 0) {
			this.lives --;
            updatePlayerSidebar(this);
		}

	},
	addKills: function() {
		this.kills ++;
        updatePlayerSidebar(this);
	},
	isCurrentPlaceFree: function() {
		result = getItemIndexByPos(bombs, this.getPosX(), this.getPosY());
		return result == -1 ? true : false;
	},
	plantBomb: function() {
		if (this.isCurrentPlaceFree()) {
			// undefined bombs will be overwrote by active bomb
			var bombAdded = false;
			result = bombs.indexOf(undefined);
			if (result != -1) {
				bombs[result] = new Bomb(this.getPosX(), this.getPosY(), this);
				bombAdded = true;
			}
			if (!bombAdded) {
				bombs.push(new Bomb(this.getPosX(), this.getPosY(), this));
			}
			this.currentBombsNumber ++;
		}
	},
	assignKeys: function(keys){
		this.assingedKeys = keys;
	},

	update: function() {
		if (this.x_move > 0) {
			this.x_move -= this.speed;
			this.x += this.speed;
			if (this.x_move < 0) {
				this.x += this.x_move;
				this.x_move = 0;
			}
		}
		if (this.x_move < 0) {
			this.x_move += this.speed;
			this.x -= this.speed;
			if (this.x_move > 0) {
				this.x += this.x_move;
				this.x_move = 0;
			}
		}
		if (this.y_move > 0) {
			this.y_move -= this.speed;
			this.y += this.speed;
			if (this.y_move < 0) {
				this.y += this.y_move;
				this.y_move = 0;
			}
		}
		if (this.y_move < 0) {
			this.y_move += this.speed;
			this.y -= this.speed;
			if (this.y_move > 0) {
				this.y += this.y_move;
				this.y_move = 0;
			}
		}
	},
	draw: function() {
		if (this.id == 2){
			//console.log("Drawing computer to: " +  this.x + " " + this.y);
		}
		try {
			ctx.drawImage(images["TilesSprite2"], grid * 1, 0, grid, grid, this.x, this.y, grid, grid);
		} catch(e) {
			alert(e);
		}
	}
});